﻿using System.Collections.Generic;
using System.Diagnostics;
using FreshTexture.Interfaces;
using FreshTexture.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace FreshTexture.ViewModel
{
    /// <summary>
    ///     This class contains properties that the main View can data bind to.
    ///     <para>
    ///         See http://www.galasoft.ch/mvvm
    ///     </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        ///     The <see cref="SourceTexturePack" /> property's name.
        /// </summary>
        public const string SourceTexturePackPropertyName = "SourceTexturePack";

        /// <summary>
        ///     The <see cref="DestinationTexturePack" /> property's name.
        /// </summary>
        public const string DestinationTexturePackPropertyName = "DestinationTexturePack";

        private List<Entity> _destinationTexturePack;
        private RelayCommand _exitCommand;
        private RelayCommand _loadCommand;
        private RelayCommand _saveCommand;

        private List<Entity> _sourceTexturePack;
        private IDataService myDataService;


        public MainViewModel(IDataService dataservice)
        {
            myCommands = new Commands();

            myDataService = dataservice;
            myDataService.GetList(lista => { SourceTexturePack = lista; },
                lista2 => { DestinationTexturePack = lista2; });
        }

        private Commands myCommands { get; set; }

        /// <summary>
        ///     Sets and gets the SourceTexturePack property.
        ///     Changes to that property's value raise the PropertyChanged event.
        /// </summary>
        public List<Entity> SourceTexturePack
        {
            get { return _sourceTexturePack; }

            set
            {
                if (_sourceTexturePack == value)
                {
                    return;
                }

                RaisePropertyChanging(SourceTexturePackPropertyName);
                _sourceTexturePack = value;
                Debug.WriteLine("property {0} raised, elements: {1} ", SourceTexturePackPropertyName,
                    SourceTexturePack.Count);
                RaisePropertyChanged(SourceTexturePackPropertyName);
            }
        }

        /// <summary>
        ///     Sets and gets the DestinationTexturePack property.
        ///     Changes to that property's value raise the PropertyChanged event.
        /// </summary>
        public List<Entity> DestinationTexturePack
        {
            get { return _destinationTexturePack; }

            set
            {
                if (_destinationTexturePack == value)
                {
                    return;
                }

                RaisePropertyChanging(DestinationTexturePackPropertyName);
                _destinationTexturePack = value;
                RaisePropertyChanged(DestinationTexturePackPropertyName);
            }
        }

        /// <summary>
        ///     Gets the LoadCommand.
        /// </summary>
        public RelayCommand LoadCommand
        {
            get
            {
                return _loadCommand
                       ?? (_loadCommand = new RelayCommand(myCommands.LoadCommand));
            }
        }

        /// <summary>
        ///     Gets the SaveCommand.
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand ?? (_saveCommand = new RelayCommand(
                    myCommands.SaveCommand,
                    myCommands.SaveCommandCanExecute));
            }
        }

        public RelayCommand ExitCommand
        {
            get
            {
                return _exitCommand ?? (_exitCommand = new RelayCommand(
                    myCommands.ExitCommand));
            }
        }


        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}